package cn.zahou.SJR.JDBCToolsc;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

@SuppressWarnings("ALL")
public class Jdbc_toolsc {
//    定义成员变量
    private  static  DataSource Dsource;
    static {

        try {
            Properties prs=new Properties();
            prs.load(Jdbc_toolsc.class.getClassLoader().getResourceAsStream("druid.properties"));
//            获取连接池对象
            Dsource= DruidDataSourceFactory.createDataSource(prs);
        } catch( Exception e) {
            e.printStackTrace();
        }
    }

//获取连接
public  static Connection getConnection() throws SQLException {
    return  Dsource.getConnection();
}
//释放资源
public  static  void  close(Statement stmt,Connection connection){
        close(null,stmt,connection);
}
public  static  void  close(ResultSet rs,Statement stmt ,Connection connection){
        if (rs!=null & stmt!=null & connection!=null){
            try {
                rs.close();
                stmt.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
}
public  static  DataSource getDataSource() {
    return  Dsource;
}
}